<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * This license is also available via the world-wide-web at
 * http://itdapps.ncsu.edu/bsd.txt
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to itappdev@ncsu.edu so we can send you a copy immediately.
 *
 * @package    Index_Controller
 * @category   Controller
 * @copyright  Copyright (c) 2007 NC State University Office of      
 *             Information Technology
 * @license    http://itdapps.ncsu.edu/bsd.txt  BSD License
 * @version    SVN: $Id: $
 */

/**
 * Main controller for the application
 *
 * @package    Index_Controller
 * @category   Controller
 * @copyright  Copyright (c) 2007 NC State University Office of      
 *             Information Technology
 */
class TasksController extends Zend_Controller_Action
{
          
    /**
     * shows the homepage
     *
     */
    public function indexAction()
    {            
    	$tasks = new Tasks();
        $taskArray = $tasks->getTasksForAccountId(Zend_Auth::getInstance()->getIdentity()->accountId);
        $tags = new Tags();
        $tagArray = $tags->getTagsForAccountId(Zend_Auth::getInstance()->getIdentity()->accountId);
        
        $addTaskForm = $tasks->form();
        $addTagForm = $tags->form();
        
        if ($this->_request->isPost()) {
        	if ($addTaskForm->isValid($_POST)) {
        		
        		$data = array(
        		    'accountId'      	=> Zend_Auth::getInstance()->getIdentity()->accountId,
        		    'taskName'			=> $addTaskForm->getValue('taskName'),
        			'description'		=> $addTaskForm->getValue('description'),
        		    'created' 			=> time(),
        			'status'			=> 'active'
        		);

        		
        		$dba = $tasks->getAdapter();
        		
        		$dba->beginTransaction();
        		
        		try {
        		    $taskId = $tasks->insert($data);
        		} catch (Exception $e) {
        			$dba->rollback();
        			throw $e;
        		}
        		                     
	            $dba->commit();
	            
	            $this->_helper->redirector->setPrependBase('')
                	->gotoUrl($this->view->url(array('module' => 'default', 'controller' => 'tasks', 'action' => 'index'), 'default', true) . '#tasks');
        	}
        	if ($addTagForm->isValid($_POST)) {
        		
        		$data = array(
        		    'accountId'      	=> Zend_Auth::getInstance()->getIdentity()->accountId,
        		    'name'				=> $addTagForm->getValue('name'),
        			'description'		=> $addTagForm->getValue('description'),
        		    'parentTagId' 		=> $addTagForm->getValue('parentTagId')
        		);

        		
        		$dba = $tags->getAdapter();
        		
        		$dba->beginTransaction();
        		
        		try {
        		    $tagId = $tags->insert($data);
        		} catch (Exception $e) {
        			$dba->rollback();
        			throw $e;
        		}
        		                     
	            $dba->commit();
	            
	            $this->_helper->redirector->setPrependBase('')
                	->gotoUrl($this->view->url(array('module' => 'default', 'controller' => 'tasks', 'action' => 'index'), 'default', true) . '#tags');
        	}
        }
        $this->view->addTaskForm = $addTaskForm;   
        $this->view->addTagForm = $addTagForm;      
        
        $this->view->taskArray = $taskArray;
        $this->view->tagArray = $tagArray;
        $this->_helper->pageTitle('My Tasks');
    }
    
	public function editTaskAction()
    {       
        $get = Zend_Registry::get('getFilter');

        $tasks = new Tasks();
        
        $thisTask = $tasks->find($get->taskId);
        $form = $tasks->form($thisTask->toArray());
        
        $messages = array();
        
        if ($this->_request->isPost()) {
            if ($form->isValid($_POST)) {
                
                $data = array(
                    'taskId'      	=> $thisTask->taskId,
                    'taskName'		=> $form->getValue('taskName'),
                    'description'   => $form->getValue('description')
                );
                
                $dba = $tasks->getAdapter();
                
                $dba->beginTransaction();
                
                try {
                    $tasks->update($data, null);
                } catch (Exception $e) {
                    $dba->rollback();
                    throw $e;
                }
		                        
		        $logOptions = array(
		         	'attributeName' => 'taskId',
		          	'attributeId'   => $thisTask->taskId,
		        );
                
                $this->_helper->log(Zend_Log::INFO, 'Task ' . $thisTask->taskId . ' was modified', $logOptions);     
                $this->_helper->flashMessenger->addMessage('The task was successfully modified.');
                
                $this->_helper->redirector->gotoRoute(array('module' => 'default', 'controller' => 'tasks', 'action' => 'index'), 'default', true);
            } else {
                $messages[] = 'There was an error modifying this task.';
            }
        }
        
        $this->view->form = $form;
        $this->view->messages = $messages;
        $this->_helper->pageTitle('Edit Task : ' . $thisTask->taskName);
    }
    
    public function deleteTaskAction()
    {
    	$get = Zend_Registry::get('getFilter');
    	$tasks = new Tasks();
    	
    	if(!$tasks->isTaskOwner(Zend_Auth::getInstance()->getIdentity()->accountId, $get->taskId)) {
    		throw new Ot_Exception_Data('You are not the owner of this task.');
    	}
    	
    	$thisTask = $tasks->find($get->taskId);
    	$taskArray = $thisTask->toArray();
    	    	
    	$form = $tasks->deleteTaskForm($taskArray);
    	
    	foreach($taskArray as $t) {
    		$taskName = $t['taskname'];
    	}
    		    	    	
	    if ($this->_request->isPost()) {
	            if ($form->isValid($_POST)) {

	            	$tasks->delete($tasks->getAdapter()->quoteInto('taskId = ?', $form->getValue('taskId')));
			                        
			        $logOptions = array(
			         	'attributeName' => 'taskId',
			          	'attributeId'   => $form->getValue('taskId'),
			        );
	                
	                $this->_helper->log(Zend_Log::INFO, 'Task ' . $form->getValue('taskId') . ' was deleted', $logOptions);     
	                $this->_helper->flashMessenger->addMessage('The task was successfully deleted.');
	                
	                $this->_helper->redirector->gotoRoute(array('module' => 'default', 'controller' => 'tasks', 'action' => 'index'), 'default', true);
	            } else {
	                $messages[] = 'There was an error deleting this task.';
	            }
		}
    	
    	
    	$this->_helper->pageTitle('Delete Task');
    	$this->view->form = $form;
    	
    }
}